package org.bilinc.gz001;

import org.bilinc.gz001.entity.City;
import org.bilinc.gz001.entity.Position;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

public class AddPersonDialog extends JDialog {

    private JTextField mTabN = new JTextField();
    private JTextField mFIO = new JTextField();
    private JComboBox<String> mCity;
    private JComboBox<String> mOtdel;

    private boolean mIsAddedPressed = false;

    public AddPersonDialog(JFrame parent) {
        super(parent, "Добавить сотрудника", true);
        setResizable(false);
        setLocation(parent.getWidth() / 4, parent.getHeight() / 4);
    }

    public void init() {
        Box b = Box.createVerticalBox();
        b.add(Box.createGlue());

        b.add(new JLabel((String) Utils.WORKED_HEADER[0]));
        b.add(mTabN);
        b.add(new JLabel((String) Utils.WORKED_HEADER[1]));

        mCity.setEditable(false);
        mCity.setSelectedIndex(0);
        mCity.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent evt) {
                switch (mCity.getSelectedIndex()) {

                }
            }
        });
        b.add(mCity);
        b.add(new JLabel((String) Utils.WORKED_HEADER[2]));
        b.add(mFIO);
        b.add(new JLabel((String) Utils.WORKED_HEADER[3]));
        mOtdel.setEditable(false);
        mOtdel.setSelectedIndex(0);
        mOtdel.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent evt) {
                switch (mOtdel.getSelectedIndex()) {

                }
            }
        });
        b.add(mOtdel);

        getContentPane().add(b, "Center");

        JPanel p2 = new JPanel();
        JButton add = new JButton("Добавить");
        JButton cancel = new JButton("Отмена");
        p2.add(add);
        p2.add(cancel);
        getContentPane().add(p2, "South");

        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (getTabN() == null || getTabN().isEmpty()) {
                    Utils.showIncorrectData(getParent(), Utils.WORKED_HEADER[0]);
                    return;
                }
                if (getCity() == null || getCity().isEmpty()) {
                    Utils.showIncorrectData(getParent(), Utils.WORKED_HEADER[1]);
                    return;
                }
                if (getFio() == null || getFio().isEmpty()) {
                    Utils.showIncorrectData(getParent(), Utils.WORKED_HEADER[2]);
                    return;
                }
                if (getOtdel() == null || getOtdel().isEmpty()) {
                    Utils.showIncorrectData(getParent(), Utils.WORKED_HEADER[3]);
                    return;
                }
                setVisible(false);
                mIsAddedPressed = true;
            }
        });
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                setVisible(false);
                mIsAddedPressed = false;
            }
        });
        setSize(250, 300);
        setVisible(true);
    }

    public void initCity(List<City> list) {
        String[] c = new String[list.size()];

        for (int i = 0; i < list.size(); i++) {
            c[i] = list.get(i).getCity();
        }
        mCity = new JComboBox<String>(c);
    }

    public void initOtdel(List<Position> list) {
        String[] c = new String[list.size()];

        for (int i = 0; i < list.size(); i++) {
            c[i] = list.get(i).getPosition();
        }
        mOtdel = new JComboBox<String>(c);
    }

    public String getTabN() {
        return mTabN.getText();
    }

    public String getFio() {
        return mFIO.getText();
    }

    public String getCity() {
        return (String) mCity.getSelectedItem();
    }

    public String getOtdel() {
        return (String) mOtdel.getSelectedItem();
    }

    public boolean isAddBtnPressed() {
        return mIsAddedPressed;
    }
}